import java.io.*;
import java.util.Properties;

public class Test {
    public static void main(String[] args) {
        Properties props = new Properties();
        File file = new File("config.properties");

        /* 1. 读文件 */
        try (Reader reader = new FileReader(file)) {
            props.load(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }

        /* 2. 业务层读取 & 修改 */
        String user = props.getProperty("user");
        System.out.println("当前用户 = " + user);

        // 改值 + 新增
        props.setProperty("timeout", "60");
        props.setProperty("language", "zh_CN"); // 新 key

        /* 3. 写回磁盘（保留注释用 store ） */
        try (Writer writer = new FileWriter(file)) {
            props.store(writer, "Updated at " + java.time.LocalDateTime.now());
        } catch (IOException e) {
            e.printStackTrace();
        }

        /* 4. 再次读取验证 */
        props.forEach((k, v) -> System.out.println(k + " = " + v));
    }
}